package com.baizhi.mall.controller;


import com.baizhi.mall.entity.mysql.BzRoleResourceDO;
import com.baizhi.mall.service.IBzRoleResourceService;
import com.baizhi.mall.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhw
 * @since 2021-05-17
 */
@RestController
public class BzRoleResourceController {
    @Autowired
    private IBzRoleResourceService rs;
    @GetMapping("/resources")
    public ResultEntity<List<BzRoleResourceDO>> selectAll(){
        List<BzRoleResourceDO>list=rs.selectAll();
        return ResultEntity.ok(list);
    }
    @GetMapping("/resource/{roleId}")
    public ResultEntity<List<BzRoleResourceDO>> selectByRoleId(@PathVariable("roleId")Integer roleId){
        List<BzRoleResourceDO>list=rs.list();
        return ResultEntity.ok(list);
    }
    @PutMapping("resource/{roleId}")
    public ResultEntity<String>insert(@PathVariable("roleId")Integer roleId, @RequestBody int[] rolePermissions){
        rs.deleteByRole(roleId);
        for(int i=0;i<=rolePermissions.length-1;i++){
            rs.insert(new BzRoleResourceDO(0,roleId,rolePermissions[i]));
        }
        return ResultEntity.ok();
    }
}

